      1  /**

      2     A cash register totals up sales and computes change due.

      3  */

      4  public class CashRegister

      5  {

      6     private double purchase;

      7     private double payment;

      8

      9     /**

     10        Constructs a cash register with no money in it.

     11     */

     12     public CashRegister()

     13     {

     14        purchase = 0;

     15        payment = 0;

     16     }

     17

     18     /**

     19        Records the purchase price of an item.

     20        @param amount the price of the purchased item

     21     */

     22     public void recordPurchase(double amount)

     23     {

     24        purchase = purchase + amount;

     25     }

     26

     27     /**

     28        Enters the payment received from the customer.

     29        @param coinCount the number of coins received

     30        @param coinType the type of coin that was received

     31     */

     32     public void receivePayment(int coinCount, Coin coinType)

     33     {

     34        payment = payment + coinCount * coinType.getValue();

     35     }

     36

     37     /**

     38        Computes the change due and resets the machine for the next customer.

     39        @return the change due to the customer

     40     */

     41     public double giveChange()

     42     {

     43        double change = payment - purchase;

     44        purchase = 0;

     45        payment = 0;

     46        return change;

     47     }

     48  }